package com.quanyue.haircut.shop.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.haircut.domain.entity.IntegralProduct;
import com.quanyue.haircut.shop.mapper.ProductMapper;
import com.quanyue.haircut.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public IntegralProduct createProduct(IntegralProduct product) {
        product.setDelFlag(false);
        product.setCreateTime(LocalDateTime.now());
        product.setModifyTime(LocalDateTime.now());
        productMapper.insert(product);
        return product;
    }

    @Override
    public void updateProduct(IntegralProduct product) {
        IntegralProduct check = productMapper.findOne(product.getId());
        AssertUtils.notNull(check, "商品信息校检出错");
        product.setCreateTime(check.getCreateTime());
        product.setModifyTime(LocalDateTime.now());
        product.setDelFlag(false);
        productMapper.updateSelective(product);
    }

    @Override
    public void deleteProduct(Integer productId) {
        IntegralProduct check = productMapper.findOne(productId);
        AssertUtils.notNull(check, "商品信息校检出错");
        productMapper.deleteOne(productId);
    }

    @Override
    public List <IntegralProduct> findByMerchantNo(Integer merchantNo) {
        return productMapper.findByMerchantNo(merchantNo);
    }
}
